<?php

//-----------------------------------------------------------------------------//
// Class              : MD_Admin
// Modification       :
//-----------------------------------------------------------------------------//
// Date(yyyy.mm.dd)   Developer                       Action
//-----------------------------------------------------------------------------//
// 2011.11.14         QuynhTTN                        Created
class MD_Admin extends ModelBase {

    //==========================================================================
    // Function    : MD_Admin
    // Argument    :
    // ReturnValue :
    // Creator     : QuynhTTN 2010.12.14 
    // Details     : AppendData record of table
    //==========================================================================
    function MD_Admin(&$database) {
	$this->tableName = TABLE_ADMIN;
	$this->keyField = "id";
	ModelBase::ModelBase($database);
    }

    //==========================================================================
    // Function    : insert
    // Argument    :
    // ReturnValue :
    // Creator     : QuynhTTN 2010.12.14 
    // Details     : AppendData record of table
    //==========================================================================
    function insert(&$arrayData, $id_nhom) {
	$arrayData["ten_dang_nhap"] = $arrayData["ten_dang_nhap"];
	$arrayData["mat_khau"] = md5($arrayData["mat_khau"]);
	$sql = "INSERT INTO " . $this->tableName . " (ho_ten,ten_dang_nhap,mat_khau,quyen_qlkh,quyen_qlns,quyen_dnmemail,quyen_qtht,quyen_guiEmail)";
	$sql .= " VALUES('" . $arrayData["ho_ten"] . "','" . $arrayData["ten_dang_nhap"] . "','" . $arrayData["mat_khau"] . "','" . $arrayData['idquyen1'] . "','" . $arrayData['idquyen_ns'] . "','" . $arrayData['idquyen2'] . "','" . $arrayData['idquyen_ht'] . "','" . $arrayData['idquyen_sendmail'] . "');";
	$this->db->query($sql);

//ManhNN 2013.09.25 insert quyen quan ly khach hang Start
	$user_id = $this->db->insert_id();

	$lisid = $_POST['lisid'];
	if ($id_nhom == '') {
	    //Thay dau ' thanh '' khi insert du lieu vao Database
	    $ten_nhom = $this->replaceString(&$arrayData["nhom_kh"]);
	    $listFilds .= "nhom_kh,";
	    $listValues .= "'" . $ten_nhom . "',";
	    $listFilds .= "parent_id,";
	    $listValues .= "" . $arrayData['parent_id'] . ",";
	    $listFilds .= "thu_tu,";
	    $listValues .= "" . $arrayData['thu_tu'] . ",";
	    $listFilds .= "published";
	    $listValues .= "'1'";

	    $sql_nhom = "INSERT INTO bfw_nhom_kh (  $listFilds ) ";
	    $sql_nhom .= " VALUES ($listValues ); ";
	    $this->db->query($sql_nhom);

	    $id_nhom_kh = $this->db->insert_id();
	    $id_nhom_kh = ",$id_nhom_kh,";
	} else {
	    $id_nhom_kh = $id_nhom;
	}
	if ($lisid == "") {
	    $lisid_new = "";
	} else {
	    $lisid_new = ",$lisid,";
	}
	$sql_quyen = "INSERT INTO bfw_role_user_khach_hang (user_id,id_nhom_kh,id_kh)";
	$sql_quyen .= " VALUES($user_id,'$id_nhom_kh','$lisid_new');";
	$this->db->query($sql_quyen);
//ManhNN 2013.09.25 insert quyen quan ly khach hang End
    }

    //==========================================================================
    // Function    : update
    // Argument    :
    // ReturnValue :
    // Creator     : QuynhTTN 2010.12.14 
    // Details     : AppendData record of table
    //==========================================================================
    function update(&$arrayData) {
	$arrayData["go_lai_mat_khau"] = md5($arrayData["go_lai_mat_khau"]);
	$sql = "UPDATE  " . $this->tableName . " SET ";
	$sql .= "mat_khau='" . $arrayData["go_lai_mat_khau"] . "',";
	$sql .= "ho_ten='" . $arrayData["ho_ten"] . "',";
	$sql .= "ten_dang_nhap='" . $arrayData["ten_dang_nhap"] . "',";

	$sql .= "quyen_qlkh='" . $arrayData["idquyen1"] . "',";
	$sql .= "quyen_dnmemail='" . $arrayData["idquyen2"] . "',";
	$sql .= "quyen_guiEmail='" . $arrayData["idquyen_sendmail"] . "'";


	$sql .= " WHERE $this->keyField=" . $arrayData["id"];
	//echo $sql;
	$this->db->query($sql);
    }

    //==========================================================================
    // Function    : getById
    // Argument    :
    // ReturnValue :
    // Creator     :Tran Thanh 2007.06.02
    // Details     : AppendData record of table
    //==========================================================================
    function getById($id) {
	$sql = "SELECT *  FROM $this->tableName ";
	if ($id != "") {
	    $sql .= " WHERE $this->keyField =$id ";
	}
	return $this->db->retrieve($sql);
    }

}

?>